package cn.iocoder.yudao.module.call.service.listenaction.action;



import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.mybatis.core.enums.QueryCondition;
import cn.iocoder.yudao.module.call.api.form.dto.FieldRuleDTO;
import cn.iocoder.yudao.module.call.enums.FormFieldTypeEnum;
import lombok.Getter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Getter
@Service
public class CallActionFactory {

    @Resource
    private Map<String, CallAction> handlerMap;


    public CallAction getHandler(String actionCode) {
        return handlerMap.get(actionCode);
    }

    public Map<String, Map<String, FieldRuleDTO>> getFieldRuleMapList(List<String> actionList) {
        List<String> actionListDistinct = CollUtil.distinct(actionList); //去下重
        Map<String, Map<String, FieldRuleDTO>> resMap = new HashMap<>();
        for (String action : actionListDistinct) {
            resMap.put(action, getHandler(action).rulesMap());
        }
        return resMap;
    }

    /**
     * 根据rules填充param文本描述
     */
    public List<Map<Object, Object>> buildParam(String actionParams, Map<String, FieldRuleDTO> rules) {
        JSONObject actionParamsJson = JSONUtil.parseObj(actionParams);
        List<Map<Object, Object>> result = new ArrayList<>();
        for (String k : actionParamsJson.keySet()) {
            System.out.println("---debug----");
            System.out.println(k);
            System.out.println(rules);
            FieldRuleDTO rule = rules.get(k);
            Map<Object, Object> paramMap = new HashMap<>();
            paramMap.put("desc", rule.getDesc());
            if (ObjUtil.isNotEmpty(actionParamsJson.get(k))) {
                paramMap.put("value", getValueByType(actionParamsJson.get(k), rule));
            }
            result.add(paramMap);
        }
        return result;
    }

    /**
     * 根据rule的type处理值
     */
    private String getValueByType(Object input, FieldRuleDTO rule) {
        FormFieldTypeEnum typeEnum = FormFieldTypeEnum.getByType(rule.getType());
        switch (typeEnum) {
            case SELECT:
                return getValueByCondition(input, rule);
            default:
                return input.toString();
        }
    }


    /**
     * 根据Condition获取对应值
     */
    private String getValueByCondition(Object input, FieldRuleDTO rule) {
        String result = "";
        Map<Object, Object> optionsMap = rule.getOptions();
        if (optionsMap.isEmpty()) {
            return null;
        }
        Object firstKey = optionsMap.keySet().iterator().next();
        Class<?> targetType = firstKey.getClass();  //根据Options的Key类型转换一下

        switch (rule.getCondition()) {
            case QueryCondition.IN:
                List<String> resList = new ArrayList<>();
                for (Object item : (JSONArray) input) {
                    resList.add(optionsMap.get(Convert.convert(targetType, item)).toString());
                }
                result = String.join(",", resList);
                break;
            default:
                result = optionsMap.get(Convert.convert(targetType, input)).toString();
                break;
        }
        return result;
    }
}
